**********************************
*** ALTERNATIVE SPECIFICATIONS ***
**********************************

*** OLS 

	*** Load data
		use "$output/combined_data.dta", clear
		capture drop _merge
		drop *_pop
		drop if year < 1960
		reshape wide $datasets, i(ccode country) j(year)
		
	*** Cycle through regressions
		local j = 1	
		eststo clear 
		foreach data in $datasets {			
			// Customize window by dataset
			local firstyear = 1960
			if "`data'" == "pwt" | "`data'" == "wdi" {
				local lastyear = 2019
			}
			if "`data'" == "mad" {
				local lastyear = 2018
			}	
			local endyear = `lastyear' - 1 		
				forval startyear = `firstyear'(1)`endyear' {
					local outcomeyear = `lastyear'
					gen outcome = (log(`data'`outcomeyear'/`data'`startyear')/(`outcomeyear' - `startyear')) 
					gen initial = log(`data'`startyear')
					qui reg outcome initial, robust  
					preserve
						clear
						set obs 1
						tempfile file`j'
						gen measure = "`data'"
						gen beta = _b[initial]
						gen lower = _b[initial] - invttail(`e(df_r)',0.025)*_se[initial]
						gen upper = _b[initial] + invttail(`e(df_r)',0.025)*_se[initial]
						gen startyear = `startyear'
						save `file`j''
					restore
					drop outcome initial
					local ++ j
				}
		}
		clear
		local jminus1 = `j' - 1
		forval i = 1/`jminus1' {
			append using `file`i''
		}
		gen startyear2 = startyear + .2
		gen startyear3 = startyear + .4
		#delimit ;
		tw  (rcap lower upper startyear if measure == "mad" & startyear >=1960 & startyear <= 2010, lcolor(${madcol}%30)) 	
			(rcap lower upper startyear2 if measure == "pwt" & startyear >=1960 & startyear <= 2010, lcolor(${pwtcol}%30)) 	
			(rcap lower upper startyear3 if measure == "wdi" & startyear >=1960 & startyear <= 2010, lcolor(${wdicol}%30)) 	
			(sc beta startyear if measure == "mad" & startyear >=1960 & startyear <= 2010, mcolor(${madcol}%70))	
			(sc beta startyear2 if measure == "pwt" & startyear >=1960 & startyear <= 2010, mcolor(${pwtcol}%70) msymbol(D))
			(sc beta startyear3 if measure == "wdi" & startyear >=1960 & startyear <= 2010, mcolor(${wdicol}%70) msymbol(S)), 	
			plotregion(style(none) lcolor(none)) yline(0,lcolor(black) lpattern(shortdash) lwidth(medium)) xlabel(1960(5)2010, angle(45)) 
				graphregion(fcol(white) lcol(white)) 
				ytitle("{&beta}", orientation(horizontal) size(large)) 
				xtitle("Initial Year") xsize(4) ysize(4)  ylabel(-.01(.0025).01, angle(horizontal))
				legend(order(	4 "Maddison" 
								5 "PWT"
								6 "WDI") pos(10) ring(0) col(1)
								region(lcolor(none) fcolor(none)));
		#delimit cr
		graph export "$figures/ols_mainfigure.pdf", replace
		
*** Stacked regressions 

	*** Load data
		use "$output/combined_data.dta", clear
		capture drop _merge
		drop *_pop
		drop if year < 1960
		reshape wide $datasets, i(ccode country) j(year)
		gen fixedsamp = !missing(`data'1980)
		keep if fixedsamp == 1 
			
	*** Cycle through regressions
		local j = 1	
		eststo clear 
		foreach data in pwt {			
			// Customize window by dataset
			local firstyear = 1980
			if "`data'" == "pwt" | "`data'" == "wdi" {
				local lastyear = 2019
			}
			if "`data'" == "mad" {
				local lastyear = 2018
			}	
			local endyear = `lastyear' - 1 		
				forval startyear = `firstyear'(1)`endyear' {
					local outcomeyear = `lastyear'
					gen outcome`startyear' = (log(`data'`outcomeyear'/`data'`startyear')/(`outcomeyear' - `startyear')) 
					gen initial`startyear' = log(`data'`startyear')
					local ++ j
				}
		}
		keep ccode outcome* initial*
		duplicates drop 
		duplicates report ccode
		drop if missing(initial1980)
		reshape long outcome, i(ccode) j(year)
		forval i = 1980/2018 {
			gen cons`i' = year == `i'
			replace initial`i' = 0 if year != `i'
		}
		reg outcome cons* initial*, noconstant cl(ccode) robust
		forval i = 1980/2018 {
			gen beta`i' = _b[initial`i']
			gen se`i' = _se[initial`i']
			gen lower`i' = _b[initial`i'] - invttail(`e(df_r)',0.025)*_se[initial`i']
			gen upper`i' = _b[initial`i'] + invttail(`e(df_r)',0.025)*_se[initial`i']
			if `i' != 1980 {
				qui test initial`i'=initial1980 
				gen pvalue1980`i' = `r(p)' 
			}		
		}
		local nlsurtext = ""
		forval i = 1980/2018 {
			local nlsurtext = "`nlsurtext' " + "{b0`i'=1}*cons`i' - (1 - exp(-1*{b1`i'=0.00}*(2019- `i')))/(2019 - `i')*initial`i'"
			if `i' != 2018 {
				local nlsurtext = "`nlsurtext' +" 
			}
		}
		nl (outcome = `nlsurtext'), vce(cluster ccode)
		forval i = 1980/2018 {
			gen nlbeta`i' = _b[/b1`i']
			gen nlse`i' = _se[/b1`i']
			gen nllower`i' = _b[/b1`i'] - invttail(`e(df_r)',0.025)*_se[/b1`i']
			gen nlupper`i' = _b[/b1`i'] + invttail(`e(df_r)',0.025)*_se[/b1`i']
			if `i' != 1980 {
				qui test _b[/b1`i']=_b[/b11980]
				gen nlpvalue1980`i' = `r(p)' 
			}
		}
		keep *beta* *lower* *upper* *pvalue1980*
		gen n = 1 
		duplicates drop
		reshape long nlbeta nllower nlupper nlpvalue1980 beta lower upper pvalue1980, i(n) j(year)
		
		rename year startyear 
		gen startyear2 = startyear + .3
		global olscol sea
		global nlscol vermillion 
		#delimit ;
		tw  (rcap lower upper startyear if startyear  <= 2010, lcolor(${olscol}%30)) 	
			(sc beta startyear if startyear  <= 2010 & pvalue1980 < .05, mcolor(${olscol}%70) msymbol(o)) 
			(sc beta startyear if startyear  <= 2010 & pvalue1980 > .05, mcolor(${olscol}%70) msymbol(oh)) 
			(sc beta startyear if startyear  ==1980, mcolor(black) msymbol(o)), 	
			plotregion(style(none) lcolor(none)) yline(0,lcolor(black) lpattern(shortdash) lwidth(medium)) xlabel(1980(5)2010, angle(45)) 
				graphregion(fcol(white) lcol(white)) 
				xtitle("Initial Year") xsize(3.5) ysize(4)  ylabel(-.01(.0025).01, angle(horizontal)) title("Ordinary Least Squares", color(black))
				legend(order(2 "{&ne} 1980 Coefficient (p-value < .05)" 3 "{&ne} 1980 Coefficient (p-value > .05)") pos(12) ring(0) col(1)
								region(lcolor(none) fcolor(none)))
				saving("$figures/stacked_ols.gph", replace)				;
		tw  (rcap nllower nlupper startyear2 if startyear  <= 2010, lcolor(${nlscol}%30)) 	
			(sc nlbeta startyear2 if startyear <= 2010 & nlpvalue1980 < .05, mcolor(${nlscol}%70) msymbol(o))
			(sc nlbeta startyear2 if startyear <= 2010 & nlpvalue1980 > .05, mcolor(${nlscol}%70) msymbol(oh))
			(sc nlbeta startyear2 if startyear ==1980, mcolor(black) msymbol(o)), 	
			plotregion(style(none) lcolor(none)) yline(0,lcolor(black) lpattern(shortdash) lwidth(medium)) xlabel(1980(5)2010, angle(45)) 
				graphregion(fcol(white) lcol(white)) 
				ytitle("{&beta}", orientation(horizontal) size(large)) 
				xtitle("Initial Year") xsize(3.5) ysize(4)  ylabel(-.01(.0025).01, angle(horizontal)) title("Non-Linear Least Squares", color(black))
				legend(order(2 "{&ne} 1980 Coefficient (p-value < .05)" 3 "{&ne} 1980 Coefficient (p-value > .05)" ) pos(6) ring(0) col(1)
								region(lcolor(none) fcolor(none)))
					saving("$figures/stacked_nls.gph", replace)	;						
		#delimit cr
		gr combine "$figures/stacked_nls.gph" "$figures/stacked_ols.gph", plotregion(style(none) lcolor(none)) graphregion(fcol(white) lcol(white)) 
		graph export "$figures/stacked_rolling.pdf", replace
		 
			
		
		
	